<?php

	namespace Phptricks\app\library\view\view_helper;

	/**
	 *  phptricks Framework 
	 *  link http://phptricks.net/Framework
	 *  date start 2014
	 * 
	 * Description of view_helper
	 *
	 * @author Abd Elfttah Ahmed <phptricks.net@gmail.com>
	 */
	use Phptricks\system\core\library\library;
	use Phptricks\system\library\PhptricksExceptions\PhptricksExceptions as Exc;
	use Phptricks\system\library\loaders\loader as loaders;
	use Phptricks\system\library\custom_fields\custom_fields as custom_fields;
	use Phptricks\system\library\view\view as view;
	use Phptricks\system\library\date_time\date_time as date_time;

	class view_helper implements library{

		private static $_instance = null;
		public $custom_fields;
		private $_load = null;
		private $_config = null;
		private $date_time = null;
		private $lang = null;

		public function __construct(){
			$this->custom_fields = custom_fields::getinstance();
			$this->_load = loaders::getinstance();
			$this->date_time = date_time::getinstance();
		}

		public static function getinstance(){
			if (self::$_instance === null) {
				self::$_instance = new \Phptricks\app\library\view\view_helper\view_helper();
			}
			return self::$_instance;
		}

		public function _initialize($config = '', $lang = ''){
			$this->_config = $config;
			$this->lang = $lang;
		}

		public function sub_word($str, $max){
			$num = 0;
			$array_str = explode(' ', $str);
			$return_str = array_slice($array_str, $num, $max);
			return implode(' ', $return_str);
		}

		public function get_date($start_date = null, $end_date = null){
			$data_return = array();
			$time_difference = $this->date_time->get_time_difference($start_date, $end_date);

			if ($time_difference['y'] > 0) {
				$data_return[] = $time_difference['y'] . " " . $this->lang['years'];
			}

			if ($time_difference['m'] > 0) {
				$data_return[] = $time_difference['m'] . " " . $this->lang['months'];
				$data_return[] = $time_difference['d'] . " " . $this->lang['days'];
			} else {
				if ($time_difference['d'] > 0) {
					$data_return[] = $time_difference['d'] . " " . $this->lang['days'];
					$data_return[] = $time_difference['h'] . " " . $this->lang['hour'];
				} else {
					if ($time_difference['h'] > 0) {
						$data_return[] = $time_difference['h'] . " " . $this->lang['hour'];
						$data_return[] = $time_difference['i'] . " " . $this->lang['minutes'];
					} else {
						if ($time_difference['i'] > 0) {
							$data_return[] = $time_difference['i'] . " " . $this->lang['minutes'];
							$data_return[] = $time_difference['s'] . " " . $this->lang['seconds'];
						} else {
							$data_return[] = $time_difference['s'] . " " . $this->lang['seconds'];
						}
					}
				}
			}
			unset($time_difference);
			return $this->lang['ago'] . " " . implode(' ' . $this->lang['and'] . ' ', $data_return);
		}

		public function get_image($img, $default = ''){
			if (!empty($img)) {
				if (\file_exists($img)) {
					return $img;
				} else {
					if (!empty($default)) {
						if (\file_exists($default)) {
							return $this->_config->public . $default;
						} else {
							$default = $this->_config->public . $default;
							return $default;
						}
					} else {
						return '';
					}
				}
			} else {
				if (!empty($default)) {
					$default = $this->_config->public . $default;
					return $default;
				} else {
					return '';
				}
			}
		}

		/**
		 * <pre>
		 * example {$sections_list|get_sections_list:$data.section_id}
		 * </pre>
		 * 
		 * @param array $array_data array list
		 * @param integer $select_id id for select
		 * @param integer $parent_id section id
		 * @param integer $num number for space
		 * @return string  <option selected value="section_id">section_name</option>
		 */
		public function get_sections_list($array_data, $select_id = 0, $parent_id = 0, $num = 0){
			$array_return = array();
			$html = '<option %s value="%s">%s</option>';
			$sub_text = '';
			switch ($num) {
				case 0:
					$sub_text = '';
					break;
				case 1:
					$sub_text = '-- ';
					break;
				case 2:
					$sub_text = '---- ';
					break;
				case 3:
					$sub_text = '------ ';
					break;
				case 4:
					$sub_text = '-------- ';
					break;
				case 5:
					$sub_text = '---------- ';
					break;
				case 6:
					$sub_text = '------------ ';
					break;
			}
			if (is_array($array_data)) {
				$num++;
				if ($parent_id > 0) {
					foreach ($array_data as $key => $value) {
						if ($value['section_sub_id'] == $parent_id) {
							$array_return[] = sprintf($html, $selected_data, $value['section_id'], $sub_text . $value['section_name']);
							$array_return[] = $this->get_sections_list($array_data, $select_id, $value['section_id'], $num);
						}
					}
				} else {
					foreach ($array_data as $key => $value) {
						if ($value['section_sub_id'] == '0') {
							$array_return[] = sprintf($html, $selected_data, $value['section_id'], $sub_text . $value['section_name']);
							$array_return[] = $this->get_sections_list($array_data, $select_id, $value['section_id'], $num);
						}
					}
				}
				$num = 0;
				return implode("\n", $array_return);
			} else {
				$num = 0;
				return '';
			}
		}

		/**
		 * 
		 * @param array $array_data
		 * @return array 
		 */
		public function get_fields_data($array_data){
//			pr($array_data,1);
			$array_return = array();
			if (is_array($array_data)) {
				foreach ($array_data as $key => $value) {
					pr($value, 1);
					$array_return[] = array(
						'name' => $value,
						'attr' => '',
					);
				}
			}
			if (count($array_return) > 0) {
				return '';
			}
			return $array_return;
		}

		public function getsettingby($array_data, $type){
			$array_return = array();
			if (\is_array($array_data)) {
				foreach ($array_data as $key => $value) {
					if (\strpos($key, $type)) {
						$array_return[$key] = $value;
					}
				}
			}
			if (\count($array_return) == 0) {
				return '';
			}
			return $array_return;
		}

		public function get_chart($array_data = array(), $element = ''){
			if (\is_array($array_data)) {
				$array_return = array();
				foreach ($array_data as $key => $value) {
					$val = '';
					if (\is_array($value)) {
						foreach ($value as $keys => $vals) {
							if ($keys == 'value') {
								$val .= $keys . ': ' . $vals . ',' . "";
							} else {
								$val .= $keys . ': "' . $vals . '",' . "";
							}
						}
						$array_return[] = \rtrim($val, ',');
					}
				}
				$array_return = '<script type="text/javascript">var data = [{' . \implode('},{', $array_return) . '}];new Chart($("' . $element . '").get(0).getContext("2d")).Doughnut(data);</script>';

				echo $array_return;
			}
			return false;
		}

		public function get_hashtag($data = '', $link){
			$data = $this->custom_fields->decode($data);

			if (is_array($data)) {
				foreach ($data as $key => $tag) {
					echo ' <a href="' . $link . $tag . '">#' . $tag . ' </a>';
				}
			}
		}

	}
	